package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.powerdb.designer.Commons;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.sqlloader.SqlLoader;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.entity.powerdb.designer.Database;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.DatabaseService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.*;


@Service("/powerdb/designer/databaseService")
public class DatabaseServiceImpl extends BaseServiceImpl<Database, Long> implements DatabaseService {

	private static final Logger LOG = LogManager.getLogger(DbConnInfoServiceImpl.class.getName());

	private static final String CLASS_NAME = DatabaseServiceImpl.class.getName();

	private static final String SQL_SELECT_DATABASE =
			SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_DATABASE");

	private static final String FIND_SQL_FOR_SAVE = "from Database t where upper(t.name)=upper(:name) AND t.versionId = " + Commons.DEF_VERSION_ID;
	private static final String FIND_SQL_FOR_UPDATE = "from Database t where upper(t.name)=upper(:name) AND t.id!=:id AND t.versionId = " + Commons.DEF_VERSION_ID;

	@Override
	@Resource(name = "/powerdb/designer/databaseDao")
	public void setDao(BaseDao<Database, Long> dao) {
		super.dao = dao;
	}

	@Override
	public int checkRepeat(Database database) {

		Map params = new HashMap();
		params.put("name", database.getName());

		String sql = FIND_SQL_FOR_SAVE;
		if (database.getId() != null) {
			sql = FIND_SQL_FOR_UPDATE;
			params.put("id", database.getId());
		}

		List list = dao.find(sql, params);
		if (list != null && list.size() > 0) {
			LOG.debug("数据库" + database.getName() + "已经存在");
			return 1;
		}
		return 0;
	}

	@Override
	public Database get(Long id, Long versionId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("versionId", versionId);
		String hql = "FROM Database e WHERE e.id = :id AND e.versionId = :versionId";
		return dao.getByHql(hql, params);
	}

	@Override
	public void copyDatabases(Database obj, Long versionId) {
		LOG.debug("copyDatabases: {},{}", obj, versionId);
		if (obj != null) {
			Database tmp = clone(obj, versionId);
			dao.save(tmp);
		}
	}

	@Override
	public List<Map<String, Object>> getDatabase(QueryFilter filter) {
		filter.setSql(SQL_SELECT_DATABASE);
		String sql = filter.createSql();
		return dao.findBySql(sql);
	}

	private Database clone(Database obj, Long versionId) {
		Database database = new Database();
		database.setVersionId(versionId);
		database.setCreateTime(obj.getCreateTime());
		database.setModifyTime(obj.getModifyTime());
		database.setCreateAccountId(obj.getCreateAccountId());
		database.setModifyAccountId(obj.getModifyAccountId());
		database.setDatabaseCode(obj.getDatabaseCode());
		database.setName(obj.getName());
		database.setNameCn(obj.getNameCn());
		database.setScript(obj.getScript());
		return database;
	}

}
